package com.gurun.camera.addDevice.presenter;

import com.danale.sdk.platform.constant.device.OnlineType;
import com.danale.sdk.platform.constant.deviceinfo.AddedState;
import com.danale.sdk.platform.constant.deviceinfo.ExistedState;
import com.danale.sdk.platform.entity.deviceinfo.DeviceAddedOnlineInfoV4;
import com.danale.sdk.platform.result.device.AddDeviceResult;
import com.danale.sdk.throwable.PlatformApiError;
import com.gurun.camera.R;
import com.gurun.camera.account.activity.AccountUtil;
import com.gurun.camera.addDevice.model.AddDeviceModel;
import com.gurun.camera.addDevice.model.AddDeviceModelImpl;
import com.gurun.camera.addDevice.view.AddDeviceView;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by Administrator on 2016/12/22.
 */

public class AddDevicePresenterImpl implements IAddDevicePresenter {
    private AddDeviceModel mModel;
    private AddDeviceView mView;

    public AddDevicePresenterImpl(AddDeviceView view){
        this.mView = view;
        mModel = new AddDeviceModelImpl();
    }

    /**
     * Request the added status of the device and the offline status
     * @param deviceId
     */
    @Override
    public void checkDevStatus(String deviceId) {
        mModel.checkDeviceState(deviceId)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<DeviceAddedOnlineInfoV4>() {
                    @Override
                    public void call(DeviceAddedOnlineInfoV4 deviceAddedOnlineInfoV4) {
                        handleStatus(deviceAddedOnlineInfoV4);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if( throwable instanceof PlatformApiError)
                            if(mView != null){
                                mView.onDeviceStatusCheckFailed(((PlatformApiError)throwable).getErrorDescription());
                            }
                    }
                });
    }

    /**
     * add Device
     * @param device deviceID
     * @param alias device alias
     */

    @Override
    public void addDevice(String device, String alias) {
        mModel.addDevice(device, alias)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<AddDeviceResult>() {
                    @Override
                    public void call(AddDeviceResult addDeviceResult) {
                        if (mView != null) {
                            mView.onAddDevSuccess();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        if (throwable instanceof PlatformApiError) {
                            if (mView != null) {
                                mView.onAddDevFailed(((PlatformApiError)throwable).getErrorDescription());
                            }
                        }
                    }
                }, new Action0() {
                    @Override
                    public void call() {

                    }
                });
    }


    private void handleStatus(DeviceAddedOnlineInfoV4 status){
        if(status.getExistedState() == ExistedState.ID_NOT_EXISTED){
            if(mView != null){
                mView.onDeviceIdIllegal(AccountUtil.getInstance(null).getAppContext().getString(R.string.device_not_exist));
            }
        }else {
            AddedState addedState = status.getAddedState();
            if(addedState == AddedState.OTHER_ADDED){
                if(mView != null){
                    mView.onDeviceAddedByOther(status.getDeviceId(),status.getOwnerName());
                }
            }else if(addedState == AddedState.SELF_ADDED){
                if(mView != null){
                    mView.onDeviceAddedByMyself();
                }
            }else {
                OnlineType onlineType = status.getOnlineType();
                if(onlineType == OnlineType.OFFLINE || onlineType == OnlineType.OTHER){
                    if(mView != null){
                        mView.onDeviceOffline(status.getDeviceId());
                    }
                }else if(onlineType == OnlineType.ONLINE){
                    if(mView != null){
                        mView.onDeviceCanAdd(status.getDeviceId());
                    }
                }
            }
        }
    }
}
